//
//  ByIndexHealthDetailVC.m
//  jinpuyiyuan_ios
//
//  Created by pro on 2017/12/12.
//  Copyright © 2017年 mc. All rights reserved.
//

#import "ByIndexHealthDetailVC.h"

// model
#import "ByModelIndexHealthDetail.h"

@interface ByIndexHealthDetailVC ()
@property (nonatomic, strong) UIScrollView *scroll;
@property (nonatomic, strong) ByModelIndexHealthDetail *detailModel;
@end

@implementation ByIndexHealthDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"文章";
    [self addLeftBackBarButtonItem];
    [self fetchData];
}

#pragma mark - getter
- (UIScrollView *)scroll{
    if (!_scroll) {
        _scroll = [[UIScrollView alloc] init];
        [self.view addSubview:_scroll];
        [_scroll mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(0));
            make.right.equalTo(@(0));
            if (@available(iOS 11.0,*)) {
                make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
                make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
            }else{
                make.top.equalTo(self.view.mas_top);
                make.bottom.equalTo(self.view.mas_bottom);
            }
        }];
    }
    return _scroll;
}

#pragma mark -
- (void)fetchData{
    self.detailModel = [[ByModelIndexHealthDetail alloc] init];
    self.detailModel.title = @"舒经络，补血气，适合冬天喝哦！";
    self.detailModel.keys = @[@"舒适",@"健康",@"活血",@"测试测试测试",@"哈哈哈哈哈",];
    self.detailModel.author = @"博也测试";
    self.detailModel.detail = @"初始登记法国红酒申达股份换公司的回复根深蒂固烦回家睡个好觉发多个说减肥几十个沪电股份时间大概返回数据个是代发货公司的会更烦三个地方是绝地反击还是搞到返回格式的福建高速初始登记法国红酒申达股份换公司的回复根深蒂固烦回家睡个好觉发多个说减肥几十个沪电股份时间大概返回数据个是代发货公司的会更烦三个地方是绝地反击还是搞到返回格式的福建高速初始登记法国红酒申达股份换公司的回复根深蒂固烦回家睡个好觉发多个说减肥几十个沪电股份时间大概返回数据个是代发货公司的会更烦三个地方是绝地反击还是搞到返回格式的福建高速初始登记法国红酒申达股份换公司的回复根深蒂固烦回家睡个好觉发多个说减肥几十个沪电股份时间大概返回数据个是代发货公司的会更烦三个地方是绝地反击还是搞到返回格式的福建高速初始登记法国红酒申达股份换公司的回复根深蒂固烦回家睡个好觉发多个说减肥几十个沪电股份时间大概返回数据个是代发货公司的会更烦三个地方是绝地反击还是搞到返回格式的福建高速初始登记法国红酒申达股份换公司的回复根深蒂固烦回家睡个好觉发多个说减肥几十个沪电股份时间大概返回数据个是代发货公司的会更烦三个地方是绝地反击还是搞到返回格式的福建高速";
    [self createViews];
}

#pragma mark - create views
- (void)createViews{
    // 1.图片
    UIImageView *infoImg = [[UIImageView alloc] init];
    [self.scroll addSubview:infoImg];
    [infoImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(0));
        make.top.equalTo(@(0));
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, SCREEN_WIDTH / 2));
    }];
    infoImg.backgroundColor = BY_THEME_COLOR;
    
    // 2.标题
    UILabel *titleLabel = [[UILabel alloc] init];
    [self.scroll addSubview:titleLabel];
    titleLabel.numberOfLines = 0;
    titleLabel.font = FONT(14);
    CGFloat height = [self.detailModel.title getRectOfStringWithSize:
                      CGSizeMake(SCREEN_WIDTH - 20, MAXFLOAT) font:14].size.height;
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(infoImg.mas_bottom).offset(10);
        make.left.equalTo(@(10));
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - 20, height));
    }];
    titleLabel.text = self.detailModel.title;
    
    // 关键字
    float xOffset = 10.0;
    float yOffset = 10;
    for (int i = 0; i < self.detailModel.keys.count; i++) {
        NSString *strTitle = self.detailModel.keys[i];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        CGFloat title_width = [strTitle getSizeOfString:14].width + 30;
        if (xOffset + title_width > SCREEN_WIDTH - 20) {
            xOffset = 10;
            yOffset += 30 + 5;
        }
        [self.scroll addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(xOffset));
            make.top.equalTo(titleLabel.mas_bottom).offset(yOffset);
            make.size.mas_equalTo(CGSizeMake(title_width, 30));
        }];
        xOffset += title_width + 8;
        btn.tag = 100 + i;
        btn.layer.cornerRadius = 4.0;
        [btn setTitle:strTitle forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn.titleLabel setFont:FONT(14)];
        [btn setBackgroundColor:[UIColor whiteColor]];
        btn.layer.shadowOffset = CGSizeMake(1, 1);
        btn.layer.shadowRadius = 5.0;
        btn.layer.shadowColor = [UIColor colorWithWhite:0.90 alpha:1].CGColor;
        btn.layer.shadowOpacity = 0.8;
    }
    
    // 作者相关
    UIButton *lastButton = (UIButton *)self.scroll.subviews.lastObject;
    UIImageView *icon = [[UIImageView alloc] init];
    [self.scroll addSubview:icon];
    [icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(10));
        make.top.equalTo(lastButton.mas_bottom).offset(15);
        make.size.mas_equalTo(CGSizeMake(35, 35));
    }];
    icon.backgroundColor = BY_THEME_COLOR;
    
    UILabel *countLabel = [[UILabel alloc] init];
    [self.scroll addSubview:countLabel];
    CGFloat width = [self.detailModel.like_count getSizeOfString:14].width + 10;
    [countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-10));
        make.top.equalTo(icon.mas_top).offset(10);
        make.size.mas_equalTo(CGSizeMake(width, 15));
    }];
    countLabel.font = FONT(14);
    countLabel.text = self.detailModel.like_count;
    
    UIButton *likeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.scroll addSubview:likeButton];
    [likeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(icon.mas_top).offset(7.5);
        make.right.equalTo(countLabel.mas_left);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    
    
    UILabel *authorLabel = [[UILabel alloc] init];
    [self.scroll addSubview:authorLabel];
    [authorLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(icon.mas_right).offset(15);
        make.top.equalTo(icon.mas_top).offset(10);
        make.bottom.equalTo(icon.mas_bottom).offset(-10);
        make.right.equalTo(likeButton.mas_left).offset(-10);
    }];
    authorLabel.text = self.detailModel.author;
    authorLabel.font = FONT(14);
    
    // line
    UIView *line = [[UIView alloc] init];
    [self.scroll addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(10));
        make.top.equalTo(icon.mas_bottom).offset(10);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - 20, 1));
    }];
    line.backgroundColor = [UIColor colorWithWhite:0.90 alpha:1];
    
    // 详细内容
    UILabel *detailLabel = [[UILabel alloc] init];
    detailLabel.numberOfLines = 0;
    [self.scroll addSubview:detailLabel];
    NSAttributedString *detailStr = [self.detailModel.detail attributeString:14];
    height = [self.detailModel.detail getRectOfAttributeStringWithSize:
              CGSizeMake(SCREEN_WIDTH - 20, MAXFLOAT) font:14].size.height;
    [detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(10));
        make.top.equalTo(line.mas_bottom).offset(15);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - 20, height));
        make.bottom.equalTo(self.scroll.mas_bottom);
    }];
    detailLabel.text = self.detailModel.detail;
    detailLabel.font = FONT(14);
    
}




@end
